/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zto.fire.common.enu;

import org.apache.commons.lang3.StringUtils;

/**
 * yarn的job状态
 *
 * @author ChengLong 2019-5-16 09:19:56
 */
public enum YarnState {
    RUNNING("running"),
    ACCEPTED("accepted"),
    SUBMITTED("submitted"),
    FINISHED("finished"),
    FAILED("failed"),
    KILLED("killed"),
    UNDEFINED("undefined"),
    NULL(""),
    UNKONOW("unknow");

    // 状态信息
    private final String state;

    YarnState(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }

    /**
     * 根据状态字符串返回状态枚举
     *
     * @param state 状态
     * @return
     */
    public static YarnState getState(String state) {
        if (StringUtils.isBlank(state)) {
            return NULL;
        }

        switch (state.toLowerCase()) {
            case "running":
                return RUNNING;
            case "accepted":
                return ACCEPTED;
            case "submitted":
                return SUBMITTED;
            case "finished":
                return FINISHED;
            case "failed":
                return FAILED;
            case "killed":
                return KILLED;
            default:
                return NULL;
        }
    }
}
